//
//  NewFeatureViewController.swift
//  SinaWeibo
//
//  Created by Apple on 15/11/13.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let imageCount = 4
class NewFeatureViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    init() {
       let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout:layout)
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }
  
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    //MARK:UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        cell.index = indexPath.item
        return cell
    }
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / screenW)
        if page == imageCount - 1 {
            
        
        //???
         if let cell = collectionView?.visibleCells().last as? NewFeatureCell{
             cell.startAnimation()
         }
        }
        
    }
    
}



class NewFeatureCell: UICollectionViewCell {
    
    //MARK:显示图片index
    var index: Int = 0{
        didSet {
        
        iconImage.image = UIImage(named: "new_feature_\(index + 1)")
            
        }
    }
    
    //开始动画
    private func startAnimation(){
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
//                print("OK..")
        }

    }
    //按钮点击监听事件
    
    @objc private func start(){
        
//        print("用我吧")
        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewController, object: nil)

        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //布局
    private func setupUI(){
        
        contentView.addSubview(iconImage)
        contentView.addSubview(startButton)
        iconImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        startButton.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
            make.centerX.equalTo(contentView.snp_centerX)
        }
        startButton.hidden = true
        startButton.addTarget(self, action: "start", forControlEvents: .TouchUpInside)

    }
    
    //懒加载控件
    private lazy var iconImage:UIImageView = UIImageView()
    private lazy var startButton: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Normal)
        btn.setTitle("开始体验", forState: .Normal)
        return btn
    }()
}
